<template>
  <div class="hy-table">
    <div v-if="headerHandler" class="header">
      <slot name="header">
        <div class="title">{{ title }}</div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      ref="tableData"
      :data="tableDataAdd"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        align="center"
        width="60"
      />
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        label="序号"
        align="center"
        width="80"
      />
      <template v-for="propItem,index in propList">
        <el-table-column
          v-if="!propItem.show"
          :key="index"
          v-bind="propItem"
          align="center"
          show-overflow-tooltip
        >
          <template #default="scope">
            <slot
              :name="propItem.slotName"
              :row="scope.row"
            >{{ scope.row[propItem.prop] }}</slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div v-if="showFooter" class="footer">
      <slot name="footer">
        <el-pagination
          :current-page="page.currentPage"
          background
          :page-size="page.pageSize"
          :page-sizes="[10, 20, 30]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: ""
    },
    tableData: {
      type: Array,
      required: true
    },
    propList: {
      type: Array,
      required: true
    },
    showIndexColumn: {
      type: Boolean,
      default: false
    },
    headerHandler: {
      type: Boolean,
      default: false
    },
    showSelectColumn: {
      type: Boolean,
      default: false
    },
    page: {
      type: Object,
      default: () => ({ currentPage: 1, pageSize: 10, total: 0 })
    },
    showFooter: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      tableDataAdd: []
    };
  },
  watch: {
    tableData(val) {
      this.tableDataAdd = val;
      let havline = val.length % this.page.pageSize;
      if (val.length < this.page.pageSize && val.length > 0) {
        const remain = this.page.pageSize - havline;
        for (var i = 0; i < remain; i++) {
          this.tableDataAdd.push({
            index: val.length - 1,
            serialNumberCommpont: true
          });
        }
      }
    }
  },
  mounted() {},
  methods: {
    handleSizeChange(val) {
      this.$emit("handleSizeChange", val);
    },
    handleCurrentChange(val) {
      this.$emit("handleCurrentChange", val);
    },
    handleSelectionChange(row) {
      this.$emit("handleSelectionChange", row);
    }
  }
};
</script>

  <style scoped lang="scss">
@import "~@/styles/variables.scss";
.hy-table {
  position: relative;
  height: 100%;
  .el-table {
    position: relative;
    width: 100%;
    height: 89%;
    overflow: hidden;
  }
}
.header {
  display: flex;
  height: 45px;
  padding: 0 5px;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 20px;
    font-weight: 700;
  }

  .handler {
    align-items: center;
  }
}

.footer {
  margin-top: 15px;
  ::v-deep .el-pagination {
    position: absolute;
    right: 0;
    bottom: 20px;
    color: #fff;
    button {
      // background-color: #0092b8;
      background-color: transparent;
      border: 1px solid #0092b8;
      border-radius: 4px;
      color: #fff;
    }
    .el-pagination__total,
    .el-pagination__jump {
      color: #fff;
    }
    .el-pager {
      .number,
      .more {
        background-color: transparent;
        border: 1px solid #0092b8;
        border-radius: 4px;
        color: #fff;
        &:hover {
          color: #fff;
          background-color: #0092b8;
        }
      }
      .active {
        background-color: #0092b8 !important;
      }
    }
    .el-pagination__editor.el-input {
      .el-input__inner {
        height: 28px !important;
      }
    }
  }
}
</style>
